/*

w = SCWindow.new.front;

RippleDirBrowser.new(w, w.bounds);

*/

RippleDirBrowser{
	
	classvar dBLCLICK_INTERVAL = 0.2;
	var parent, listView, <currentDir, defaultDir, lastClick = 0, logger;

	*new{|parentArg, boundsArg|
		^super.new.init(parentArg, boundsArg);
	}
	
	init{|parentArg, boundsArg|
		logger = RippleLogger.new;
		logger.fileName = this.class.asString;
		if(boundsArg == nil){
			boundsArg	= 150@340;
		};
		parent = parentArg;
		parent.postln;
		listView = GUI.listView.new(parent,boundsArg);
		listView.mouseDownAction = {this.click};
		defaultDir = String.scDir ++ "/sounds/";
		currentDir = defaultDir;
		this.refresh;	
	}
	
	defaultDir_{|dirArg|
		if(this.isFolder(dirArg)){
			defaultDir = dirArg;
		}
	}
	
	defaultDir{
		^defaultDir;
	}
	
	isFolder{|dirArg|
	
		if(PathName(dirArg).isFolder && dirArg.contains(".app").not){
			^true
		};
		^false;
	}
	
	refresh{
		var dirs;
		dirs = (currentDir ++ "*")
		.pathMatch
		.collect({|p|
			var pName, ret;
			pName = PathName(p);
			if(this.isFolder(p).not,
				{
					ret = pName.fileName;
					if(ret == ""){
						ret = pName.folderName
					}
				},
				{ret = "[]" ++ pName.folderName}
			);
			ret;
		});
		// trim directory contents. doesn't account for different fonts, but better than nothing
		dirs = StringUtils.shortenStringsInArray(dirs, (listView.bounds.width / 7).asInteger);
		dirs = dirs.addFirst("[[  dbl-click to go up  ]]");
		listView.items = dirs;
		
		logger.log("dirs: " + dirs);
		^this;
	}
	
	click{
		var curTime;
		curTime = SystemClock.seconds;
		
		logger.log("this.selectedIndex: " + this.selectedIndex);
		if(curTime -lastClick < dBLCLICK_INTERVAL,
			{
				if(this.selectedIndex >= 0,
					{this.descend},
					{this.ascend}
				)
			}
		);
		lastClick = curTime;	
	}
	
	selectedIndex{
		^listView.value -1;
	}
	
	ascend{
		currentDir = PathName(currentDir).parentPath;
		logger.log("ascend. currentDir is: " + currentDir);
		this.refresh;
	}
	
	descend{
		var paths;
		paths = (currentDir ++ "*").pathMatch;
		logger.log("descend. selected path is: " + paths[this.selectedIndex]);
		if(this.isFolder(paths[this.selectedIndex]),
			{
				currentDir = paths[this.selectedIndex];
				this.refresh;
			}
		);
	}
	
	getSelectedFilePath{
		var path = (currentDir ++ "*").pathMatch[this.selectedIndex];
		if(path == nil){
			path = currentDir;
		};
		^path;
	}
	
	// wrapper methods for listview
	
	background_{|colorArg|
		listView.background_(colorArg);	
	}
	
	font_{|fontArt|
		listView.font_(fontArt);	
	}
	
	stringColor {
		^listView.getProperty(\stringColor, Color.new)
	}
	stringColor_ { arg color;
		listView.setProperty(\stringColor, color)
	}
	
	selectedStringColor {
		^listView.getProperty(\selectedStringColor, Color.new)
	}
	selectedStringColor_ { arg color;
		listView.setProperty(\selectedStringColor, color)
	}
	
	hiliteColor {
		^listView.getProperty(\hiliteColor, Color.new)
	}
	hiliteColor_ { arg color;
		listView.setProperty(\hiliteColor, color)
	}
	
	canFocus_{|val|
		listView.canFocus_(val);
	}
	
	canFocus{
		^listView.canFocus;
	}
	
	currentDir_{|dir|
		currentDir = dir;
		this.refresh;
	}
	
}